package com.bjgov.scoresys.service;


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.ObjectNotFoundException;

import com.bjgov.scoresys.dao.ScoreTreeDAO;
import com.bjgov.scoresys.dao.UserProjectDAO;
import com.bjgov.scoresys.entity.ScoreItem;
import com.bjgov.scoresys.entity.ScoreTree;
import com.bjgov.scoresys.entity.User;
import com.bjgov.scoresys.util.Page;
import com.bjgov.scoresys.util.PageUtil;
import com.bjgov.scoresys.util.Result;

public class ScoreTreeHandler {
	
	private static final Log log = LogFactory.getLog(UserProjectDAO.class);

	private static ScoreTreeDAO dao = new ScoreTreeDAO();
	
	public static  List<ScoreTree>  loadAll()
	{
		return dao.findAll();
	}
	
	public static List<ScoreTree> loadByPIDs(int[] pids) {
		return dao.findByPIDs(pids);
	}
	
	public static List<ScoreTree> loadByPID(int pid)
	{
		return dao.findByPid(pid);
	}
	
	public static ScoreTree loadById(int id)
	{
		return dao.findById(id);
	}
	
	public static void saveFrame(ScoreTree scoreTree) {
		dao.save(scoreTree);
	}
	
	/**
	 * 删除先暂时真删除，以后改成修改状态
	 * @param id
	 * @return
	 */
	public static String delFrame(int id)
	{
		String result="删除失败";
		try
		{
		List<ScoreTree> list = loadByPID(id);
		if(list!=null && list.size()>0)
		{
			return "当前项含有子顶，不能删除！";
		}
		
			ScoreTree scoreTree = loadById(id);
			dao.delete(scoreTree);
			result="删除成功";
		}
		catch(Exception e)
		{
			//TODO
		}
		return result;
	}
	
	/**
	 * 生成Option选择树
	 * @param list
	 * @param result
	 * @return
	 */
	private static String getTreeOption(List<ScoreTree> list,String result,int selectid)
	{
		String selectStr="";
		if(list!=null)
		{
			for(ScoreTree root_tree : list)
			{
				if(root_tree.getPid()==0)
				{
					if(root_tree.getId()==selectid)
					{
						selectStr = " selected=\"selected\" ";
					}
					result = result+"<option value="+root_tree.getId()+selectStr+">"+"┟"+root_tree.getCateName()+"</option>";
					selectStr="";
				}
				else
				{
					if(root_tree.getId()==selectid)
					{
						selectStr = " selected=\"selected\" ";
					}
					result = result+"<option value="+root_tree.getId()+selectStr+">"+"┟"+getShowStr(root_tree.getDeepPath())+root_tree.getCateName()+"</option>";
					selectStr="";
				}
				log.debug("获取catename="+root_tree.getCateName()+",_result="+result);
				List<ScoreTree> sub_list = loadByPID(root_tree.getId());
				if(sub_list!=null)
				{
					result=getTreeOption(sub_list,result,selectid);
				}
				else
				{
					 break;
				}
			}
		}
		return result;
	}
	
	/**
	 * 生成Option选择树
	 * @param list
	 * @param result
	 * @return
	 */
	private static String getTreeOption(List<ScoreTree> list,String result, String ignoreTopIds)
	{
		if(list!=null)
		{
			for(ScoreTree root_tree : list)
			{
				boolean disabled = (("," + ignoreTopIds + ",").indexOf("," + root_tree.getDeepPath().split(",")[0] + ",")!=-1);
				if(root_tree.getPid()==0)
				{
					result = result+"<option "+(disabled?"disabled":"")+" value="+root_tree.getId()+">"+"┟"+root_tree.getCateName()+"</option>";
				}
				else
				{
					result = result+"<option "+(disabled?"disabled":"")+" value="+root_tree.getId()+">"+"┟"+getShowStr(root_tree.getDeepPath())+root_tree.getCateName()+"</option>";
				}
				log.debug("获取catename="+root_tree.getCateName()+",_result="+result);
				List<ScoreTree> sub_list = loadByPID(root_tree.getId());
				if(sub_list!=null)
				{
					result=getTreeOption(sub_list,result, ignoreTopIds);
				}
				else
				{
					 break;
				}
			}
		}
		return result;
	}
	
	/**
	 * 生成选择评分子项checkbox
	 * @param list
	 * @param result
	 * @param ignoreTopIds
	 * @return
	 */
	private static String getTreeCheckBox(List<ScoreTree> list,String result, String ignoreTopIds)
	{
		if(list!=null)
		{
			for(ScoreTree root_tree : list)
			{
				boolean disabled = (("," + ignoreTopIds + ",").indexOf("," + root_tree.getDeepPath().split(",")[0] + ",")!=-1);
				if(root_tree.getPid()==0)
				{
					result = result+"<input type=checkbox "+(disabled?"disabled":"")+" name=scoreitem value="+root_tree.getDeepPath()+" onclick=selectItem('"+root_tree.getDeepPath()+"'); >"+"┟"+root_tree.getCateName()+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=javascript:void(0) onclick=selectItemCancel('"+root_tree.getDeepPath()+"')>"+(disabled?"":"取消选中")+"</a></br>";
				}
				else
				{
					result = result+"<input type=checkbox "+(disabled?"disabled":"")+" name=scoreitem value="+root_tree.getDeepPath()+" onclick=selectItem('"+root_tree.getDeepPath()+"'); />"+"┟"+getShowStr(root_tree.getDeepPath())+"<font color="+(root_tree.getHasScoreItem()?"green":"red")+">"+root_tree.getCateName()+"</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=javascript:void(0) onclick=selectItemCancel('"+root_tree.getDeepPath()+"')>"+(disabled?"":"取消选中")+"</a></br>";
				}
				log.debug("获取catename="+root_tree.getCateName()+",_result="+result);
				List<ScoreTree> sub_list = loadByPID(root_tree.getId());
				if(sub_list!=null)
				{
					result=getTreeCheckBox(sub_list,result, ignoreTopIds);
				}
				else
				{
					 break;
				}
			}
		}
		return result;
	}
	
	
	/**
	 * 返回选择option树
	 * @return
	 */
	public static String getTreeOptionString()
	{
		List<ScoreTree> root_list = ScoreTreeHandler.loadByPID(0);
	    return ScoreTreeHandler.getTreeOption(root_list,"", "");
	}
	
	/**
	 * 返回选择option树，并设置某些顶级类别不可用
	 * @ignoreTopIds 不可选的类别
	 * @return
	 */
	public static String getTreeOptionString(String ignoreTopIds) {
		List<ScoreTree> root_list = ScoreTreeHandler.loadByPID(0);
	    return ScoreTreeHandler.getTreeOption(root_list,"",ignoreTopIds);
	}
	
	/**
	 *返回选择checkbox树,并设置某些顶级类别不可用
	 * @param ignoreTopIds不可选的类别
	 * @return
	 */
	public static String getTreeCheckBoxString(String ignoreTopIds) {
		List<ScoreTree> root_list = ScoreTreeHandler.loadByPID(0);
	    return ScoreTreeHandler.getTreeCheckBox(root_list,"",ignoreTopIds);
	}
	
	/**
	 * 生成页面显示List
	 * @param list
	 * @param resultList
	 * @return
	 */
	private static List<ScoreTree> getTreeList(List<ScoreTree> list,List<ScoreTree> resultList)
	{
		if(list!=null)
		{
			for(ScoreTree root_tree : list)
			{
				resultList.add(root_tree);
				List<ScoreTree> sub_list = loadByPID(root_tree.getId());
				if(sub_list!=null)
				{
					resultList=getTreeList(sub_list,resultList);
				}
				else
				{
					 break;
				}
			}
		}
		return resultList;
	}
	
	/**
	 * 返回选择option树
	 * @return
	 */
	public static String getTreeOptionString(int selectid)
	{
		List<ScoreTree> root_list = ScoreTreeHandler.loadByPID(0);
	    return ScoreTreeHandler.getTreeOption(root_list,"",selectid);
	}
	
	public static List<ScoreTree> getTreePageList()
	{
		List<ScoreTree> list = ScoreTreeHandler.loadByPID(0);	    
	    List<ScoreTree> resultList = new ArrayList<ScoreTree>();
	    return ScoreTreeHandler.getTreeList(list,resultList);
	}
	
	/*
	 * 显示层级
	 */
	private static String getShowStr(String deepPath)
	{
		String temp="──";
		if(deepPath.split(",").length>1)
		{
			for(int i=0;i<deepPath.split(",").length-1;i++)
			{
				temp=temp+"──";
			}	
		}
		return temp;
	}
	
	/*
	 * 获取MaxID
	 */
	public static int getMaxID()
	{
		int result = dao.getMaxID();
		if(result==0)
		{
			return result;
		}
		return result;
	}

	public static ScoreTree getControlTreeByTree(int treeId) {
		ScoreTree tree = loadById(treeId);
		if(tree==null) return null;
		if(!tree.getIsControl()) {
			return getControlTreeByTree(tree.getPid());
		}else{
			return tree;
		}
	}
	
	public static ScoreTree getControlTreeByItem(int itemId) {
		ScoreItem item = ScoreSubItemsHandler.loadByID(itemId);
		return getControlTreeByTree(item.getScoreTree().getId());
	}
	
	public static Result list(Page page) throws HibernateException,
			ObjectNotFoundException {
		int totalRecords = dao.count();
		page = PageUtil.createPage(page, totalRecords);
		List list = dao.getByPage(page);
		return new Result(list, page);
	}		
}
